package org.carshared.principal;

import java.lang.reflect.Type;
import java.util.ArrayList;

import org.carshared.R;
import org.carshared.utils.PassengerBeen;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Spinner;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

public class DetailTravelActivity extends Activity {

	private String idaVuelta;
	private Spinner recorrido;
	private Spinner pasajeros;
	private boolean inicio = true;
	private ArrayList<PassengerBeen> listaPasajerosViaje;
	private ArrayList<PassengerBeen> aux;
	private String user;
	private String strJSON;
	Gson gson;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_dialog_detailtravel);

		recorrido = (Spinner) findViewById(R.id.Ntrayecto);
		pasajeros = (Spinner) findViewById(R.id.Nombrepasajeros);
		// listaNombresPasajeros = new ArrayList<String>();
		listaPasajerosViaje = new ArrayList<PassengerBeen>();
		listaPasajerosViaje.add(new PassengerBeen("Selecciona uno"));
		Bundle bundle = getIntent().getExtras();
		// listaNombresPasajeros = bundle.getStringArrayList("listaNombres");
		strJSON = bundle.getString("listaPasajeros");
		Type lstT = new TypeToken<ArrayList<PassengerBeen>>() {
		}.getType();
		gson = new Gson();
		aux = gson.fromJson(strJSON, lstT);
		listaPasajerosViaje.addAll(aux);
		user = bundle.getString("user");

		ArrayAdapter<PassengerBeen> adapter = new ArrayAdapter<PassengerBeen>(
				this, android.R.layout.simple_spinner_item, listaPasajerosViaje);

		pasajeros.setAdapter(adapter);

		if ("owner".equals(user)) {

			pasajeros.setOnItemSelectedListener(new OnItemSelectedListener() {

				@Override
				public void onItemSelected(AdapterView<?> parentView,
						View selectedItemView, int position, long id) {
					if (!inicio) {
						idaVuelta = recorrido.getSelectedItem().toString();
						PassengerBeen pasajeroSelected = (PassengerBeen) pasajeros
								.getSelectedItem();
						pasajeroSelected.setPagado(false);
						Intent returnIntent = new Intent();
						returnIntent.putExtra("dataTravel", idaVuelta);

						returnIntent.putExtra("Pasajero",
								gson.toJson(pasajeroSelected));
						setResult(RESULT_OK, returnIntent);
						DetailTravelActivity.this.finish();
					} else {
						inicio = false;
					}

				}

				@Override
				public void onNothingSelected(AdapterView<?> parentView) {
					// TODO Auto-generated method stub

				}
			});

		} else {
			pasajeros.setVisibility(View.INVISIBLE);
			findViewById(R.id.pasajeros).setVisibility(View.INVISIBLE);
			recorrido.setOnItemSelectedListener(new OnItemSelectedListener() {

				@Override
				public void onItemSelected(AdapterView<?> parentView,
						View selectedItemView, int position, long id) {
					if (!inicio) {
						idaVuelta = recorrido.getSelectedItem().toString();
						Intent returnIntent = new Intent();
						returnIntent.putExtra("dataTravel", idaVuelta);
						returnIntent.putExtra("Pasajero", "");
						setResult(RESULT_OK, returnIntent);
						DetailTravelActivity.this.finish();
					} else {
						inicio = false;
					}

				}

				@Override
				public void onNothingSelected(AdapterView<?> parentView) {
					// TODO Auto-generated method stub

				}
			});
		}

	}

}
